package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.OperationDiaryAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.OperationDiary;
import com.isunland.managesystem.entity.OperationDiaryListOriginal;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能: 操作日志列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/1/4 19:27
 */

public class LocaleWorkQueryCountDiaryListFragment extends BaseListFragment {
  public static final String EXTRA_ID = "com.isunland.intelligentFarmbyWJ.EXTRA_ITEM";
  public static final String EXTRA_STATUS =
      LocaleWorkQueryCountDiaryListFragment.class.getSimpleName() + "EXTRA_STATUS";
  private static final String EXTRA_TITLE =
      LocaleWorkQueryCountDiaryListFragment.class.getSimpleName() + "EXTRA_TITLE";
  private static final int REQUEST_ADD = 1;
  private String mId;
  private ArrayList<OperationDiary> mList;
  private OperationDiaryAdapter<OperationDiary> mAdapter;

  public static LocaleWorkQueryCountDiaryListFragment newInstance(String id, String status,
      String title) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    bundle.putString(EXTRA_STATUS, status);
    bundle.putString(EXTRA_TITLE, title);
    LocaleWorkQueryCountDiaryListFragment fragment = new LocaleWorkQueryCountDiaryListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  public static LocaleWorkQueryCountDiaryListFragment newInstance(String id, String status) {
    return LocaleWorkQueryCountDiaryListFragment.newInstance(id, status, null);
  }

  public static LocaleWorkQueryCountDiaryListFragment newInstance(String id) {
    return LocaleWorkQueryCountDiaryListFragment.newInstance(id, null, null);
  }

  //初始化数据
  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mActivity = ((BaseVolleyActivity) getActivity());
    mId = getArguments().getString(EXTRA_ID);
    //String mStatus = getArguments().getString(EXTRA_STATUS);
    String title = getArguments().getString(EXTRA_TITLE);
    if (!TextUtils.isEmpty(title)) {
      setTitleCustom(title);
    }
    mList = new ArrayList<>();

    mAdapter = new OperationDiaryAdapter<>(mActivity, mList);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setListAdapter(mAdapter);
  }

  @Override public String getUrl() {
    return ApiConst.URL_GET_LIST_ORDER_OPERATION_DIARY;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainId", mId);
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "DESC");
    return params;
  }

  @Override public void resolveResponse(String json) {
    OperationDiaryListOriginal original =
        new Gson().fromJson(json, OperationDiaryListOriginal.class);
    if (original == null || original.getRows() == null || original.getResult() != Base.SUCCESS) {
      ToastUtil.show("获取列表失败");
      return;
    }
    mList.clear();
    mList.addAll(original.getRows());
    mAdapter.notifyDataSetChanged();
  }

  //@Override
  //public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //    super.onCreateOptionsMenu(menu, inflater);
  //    if (MyStringUtil.equals(mStatus, DataStatus.NEW) || MyStringUtil.equals(mStatus, DataStatus.NEWBACK)) {
  //        inflater.inflate(R.menu.menu_add, menu);
  //    }
  //}

  //@Override
  //public boolean onOptionsItemSelected(MenuItem item) {
  //    switch (item.getItemId()) {
  //        case R.id.menu_item_add:
  //            OperationSubLogUpdateActivity.newInstance(this,mActivity,REQUEST_ADD,mId);
  //            return true;
  //
  //        default:
  //            return super.onOptionsItemSelected(item);
  //    }
  //}

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_ADD) volleyPost();
  }
}
